package com.example.java2e.Controller;

import com.example.java2e.Entity.Record;
import com.example.java2e.Service.RecordService;
import com.example.java2e.Service.RouteService;
import com.example.java2e.Service.UserService;
import com.example.java2e.Service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private UserService userService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private VehicleService vehicleService;
    @PostMapping("/add_record")
    public Map<String, Object> add_record(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try{
            String id_vehicle = front_map.get("id_vehicle").toString();
            long id_route = Long.parseLong(front_map.get("id_route").toString());
            String date = front_map.get("date").toString();
            String id_driver = front_map.get("id_driver").toString();
            int direction = Integer.parseInt(front_map.get("start_or_end").toString());
            String driver_name = userService.selectDriverById(id_driver).getName();
            if(routeService.check_route_detail(id_route).size()==0){
                back_map.put("code","路线不存在！");
                return back_map;
            }
            else if(userService.selectDriverById(id_driver).getDriver_condition()==0){
                back_map.put("code","该司机已被停职！");
                return back_map;
            }
            else if(vehicleService.checkVehicleInfoDetail(id_vehicle)==null){
                back_map.put("code","车辆不存在！");
                return back_map;
            }
            else if(vehicleService.checkVehicleInfoDetail(id_vehicle).getStatus().equals("0")){
                back_map.put("code","该车辆已停用！");
                return back_map;
            }
            else{
                recordService.add_record(date,driver_name,id_driver,id_route,id_vehicle,direction);
                if(direction==1){
                    back_map.put("direction","终点发车");
                    back_map.put("code","1");
                }
                else if(direction==0){
                    back_map.put("direction","起点发车");
                    back_map.put("code","1");
                }
                return back_map;
            }
        } catch (Exception e) {
            System.out.println(e);
            back_map.put("code","error");
            return (back_map);
        }
    }
    @PostMapping("/delete_record")
    public Map<String, Object> delete_record(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try {
            long id_route = Long.parseLong(front_map.get("id_route").toString());
            String date = front_map.get("date").toString();
            int direction = Integer.parseInt(front_map.get("direction").toString());
            recordService.delete_record(id_route,date,direction);
            List<Record> records = recordService.check_record();
            for(Record r : records){
                String[]info = new String[3];
                info[0] = r.getId_route().toString();
                info[1] = r.getDate();
                info[2] = r.getStart_or_end()==1 ? "终点发车" : "起点发车";
                back_map.put(r.getId().toString(),info);
            }
        }catch(Exception e){
            back_map.put("code",e.toString());
        }
        return back_map;
    }
    @PostMapping("/change_record")
    public Map<String, Object> change_record(@RequestBody Map<String, Object> front_map){
        long old_id_route = Long.parseLong(front_map.get("old_id_route").toString());
        String old_date = front_map.get("old_date").toString();
        int old_direction = Integer.parseInt(front_map.get("old_start_or_end").toString());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id_route",old_id_route);
        map1.put("date",old_date);
        map1.put("direction",old_direction);
        delete_record(map1);
        String new_id_vehicle = front_map.get("new_id_vehicle").toString();
        long new_id_route = Long.parseLong(front_map.get("new_id_route").toString());
        String new_date = front_map.get("new_date").toString();
        String new_id_driver = front_map.get("new_id_driver").toString();
        int new_direction = Integer.parseInt(front_map.get("new_start_or_end").toString());
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id_route",new_id_route);
        map2.put("date",new_date);
        map2.put("start_or_end",new_direction);
        map2.put("id_driver",new_id_driver);
        map2.put("id_vehicle",new_id_vehicle);
        add_record(map2);
        Map<String, Object> back_map = new HashMap<>();
        back_map=check_record(map2);
        back_map.put("code","1");
        return  back_map;
    }
    @PostMapping("/check_record")
    public Map<String, Object> check_record(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try {
            List<Record> records = recordService.check_record();
            for(Record r : records){
                String[]info = new String[3];
                info[0] = r.getId_route().toString();
                info[1] = r.getDate();
                info[2] = r.getStart_or_end()==1 ? "终点发车" : "起点发车";
                back_map.put(r.getId().toString(),info);
            }
        }catch(Exception e){
            back_map.put("code",e.toString());
        }
        return back_map;
    }
    @PostMapping("/check_record_detail")
    public Map<String, Object> check_record_detail(@RequestBody Map<String, Object> front_map) {
        Map<String, Object> back_map = new HashMap<>();
        try {
            long id_route = Long.parseLong(front_map.get("id_route").toString());
            String date = front_map.get("date").toString();
            int direction = Integer.parseInt(front_map.get("direction").toString());
            Record record = recordService.check_record_detail(id_route, date, direction).get(0);
            String dir = record.getStart_or_end() == 1 ? "终点发车" : "起点发车";
            back_map.put("id_vehicle", record.getId_vehicle());
            back_map.put("id_driver", record.getId_driver());
            back_map.put("driver", record.getDriver());
            back_map.put("direction", dir);
        } catch (Exception e) {
            back_map.put("code", e.toString());
        }
        return back_map;
    }
    @PostMapping("/check_record_start")
    public Map<String, Object> check_record_start(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try {
            long id_route = Long.parseLong(front_map.get("id").toString());
            List<Record> records = recordService.check_record_start(id_route);
            for(Record r : records){
                String[]info = new String[4];
                info[0] = r.getDate();
                info[1] = r.getId_vehicle();
                info[2] = r.getId_driver();
                info[3] = r.getDriver();
                back_map.put(String.valueOf(r.getId()),info);
            }
        }catch(Exception e){
            back_map.put("code",e.toString());
        }
        return back_map;
    }
    @PostMapping("/check_record_end")
    public Map<String, Object> check_record_end(@RequestBody Map<String, Object> front_map){
        Map<String, Object> back_map = new HashMap<>();
        try {
            long id_route = Long.parseLong(front_map.get("id").toString());
            List<Record> records = recordService.check_record_end(id_route);
            for(Record r : records){
                String[]info = new String[4];
                info[0] = r.getDate();
                info[1] = r.getId_vehicle();
                info[2] = r.getId_driver();
                info[3] = r.getDriver();
                back_map.put(String.valueOf(r.getId()),info);
            }
        }catch(Exception e){
            back_map.put("code",e.toString());
        }
        return back_map;
    }
}
